package com.jindi.appuser.common;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateConverter implements Converter<String, Date> {

	@Override
	public Date convert(String arg0) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
	    dateFormat.setLenient(false);    
	    try {    
	        return dateFormat.parse(arg0);    
	    } catch (ParseException e) {    
	        e.printStackTrace();    
	    }           
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 24小时制
	 * 时间格式化("yyyyMMddHHmmss")
	 * @param dt 时间
	 * @return
	 */
	public static String dateToString(Date dt) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(dt);
	}
	
	/**
	 * 时间格式化("yyyyMMdd")
	 * @param dt 时间
	 * @return
	 */
	public static String dateToStrings(Date dt) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(dt);
	}
	
	/**
	 * 时间格式化
	 * @param dt
	 * @param parrten 
	 * @return
	 */
	public static String dateToStrings(Date dt,String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(dt);
	}
	/**
	 * 将字符串转换成日期
	 * @param str
	 * @param pattern
	 * @return
	 */
	public static Date stringToDate(String str,String pattern) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);    
	    dateFormat.setLenient(false);    
	    try {    
	        return dateFormat.parse(str);    
	    } catch (ParseException e) {    
	        e.printStackTrace();    
	    }           
		// TODO Auto-generated method stub
		return null;
	}
	/*public static void main(String[] args) {
		System.out.println(stringToDate("2017-09-21 10:22:02", "yyyy-MM-dd HH:mm:ss"));
	}*/
	public static long dateCompareto(Date dt, Date dts) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		try {
			return sdf.parse("20170901").getTime()-sdf.parse("20170831").getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
}
